﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HYSoft.BLL;
using System.Data;
using System.Drawing;

namespace HYSoft.Web
{
    public partial class monitor_manage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Context.Session["uid"] == null || Context.Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            if (Request.QueryString["Type"] != null && Request.QueryString["ID"] != null)
            {
                string Type = Request.QueryString["Type"];  //取得操作类型

                string ID = Request.QueryString["ID"];
                switch (Type)
                {
                    case "Del":

                        Del(ID);
                        break;
                }
            }
            data_list();
        }

        protected void data_list()
        {
            int curpage = Convert.ToInt32(labPage.Text.ToString());
            PagedDataSource pg = new PagedDataSource();
            DataSet ds=new usr2monitor().GetList("[user_index]=" + int.Parse(Session["uid"].ToString()));
            DataTable dt = ds.Tables[0];
          
            pg.DataSource = ds.Tables[0].DefaultView;
            pg.AllowPaging = true;
            //每页显示的记录条数
            pg.PageSize = 5;
            pg.CurrentPageIndex = curpage - 1;
            this.lnkbtnBack.Enabled = true;
            this.lnkbtnNext.Enabled = true;
            this.lnkbtnOne.Enabled = true;
            this.lnkbtnUp.Enabled = true;
            this.lnkbtnOne.ForeColor = Color.Red;
            this.lnkbtnUp.ForeColor = Color.Red;
            this.lnkbtnNext.ForeColor = Color.Red;
            this.lnkbtnBack.ForeColor = Color.Red;
            //为当前页时，上一页和首页不可点击
            if (curpage == 1 && curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
            else if (curpage == 1)
            {
                this.lnkbtnOne.Enabled = false;
                this.lnkbtnUp.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Gray;
                this.lnkbtnUp.ForeColor = Color.Gray;
                this.lnkbtnNext.ForeColor = Color.Red;
                this.lnkbtnBack.ForeColor = Color.Red;
            }
            //为最后一页时，最后一页和下一页不可点击
            else if (curpage == pg.PageCount)
            {
                this.lnkbtnBack.Enabled = false;
                this.lnkbtnNext.Enabled = false;
                this.lnkbtnOne.ForeColor = Color.Red;
                this.lnkbtnUp.ForeColor = Color.Red;
                this.lnkbtnNext.ForeColor = Color.Gray;
                this.lnkbtnBack.ForeColor = Color.Gray;
            }
            //最后一页
            this.labBackPage.Text = Convert.ToString(pg.PageCount);
            if (dt.Rows.Count > 0)
            {
                this.ListView1.DataSource = pg;
                this.ListView1.DataBind();
            }

            dt.Clear();
            dt.Dispose();
        }
        //点击首页的事件
        protected void lnkbtnOne_Click(object sender, EventArgs e)
        {
            this.labPage.Text = "1";
            this.data_list();
        }
        //点击上一页的事件
        protected void lnkbtnUp_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) - 1);
            this.data_list();
        }
        //点击下一页的事件
        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text.ToString()) + 1);
            this.data_list();
        }
        //最后一页的事件
        protected void lnkbtnBack_Click(object sender, EventArgs e)
        {
            this.labPage.Text = labBackPage.Text;
            this.data_list();

        }

        protected void Del(string ID)
        {
            int gid = Int32.Parse(ID);
            new monitor().Delete(gid);
            var monitor_to_usermodellist = new monitor_to_user().GetModelList("[monitor_index]=" + gid);
            foreach (var item in monitor_to_usermodellist)
            {
                new monitor_to_user().Delete(item.index);
            }
            var gprsNodemodel = new gprsNode().GetModelList("areano=" + gid);
            foreach (var item in gprsNodemodel)
            {
                item.areano=-1;
                new gprsNode().Update(item);
            }
            Response.Write("<script>alert('删除监测点成功！');window.location = 'monitor_manage.aspx';</script>");
        }
    }
}